
import java.net.*;
import java.util.*;
import apmon.*;

public class UDPServer extends Thread {
	public static final int MaxRequests = 120;
	public String serverName;

	private DatagramSocket serverSocket;
	private int port;
	private ArrayList<UDPServiceThread> al;
	
	public static final String ConfigFileName = "config/mona.cfg";
	private Integer nPut=0, nGet=0, nFilter=0, nResize=0;
	private ApMon apm = null;
	
	public ArrayList<Integer> vectorIdFisiere;
	public UDPServer(int port) {
		this.port = port;
		this.al = new ArrayList<UDPServiceThread>();
		vectorIdFisiere = new ArrayList<Integer>();
		
		try {
			//apm = new ApMon(ConfigFileName);
		} catch (Exception e) {
			System.err.println("Eroare creare instanta ApMon: " + e);
			e.printStackTrace();
		}
	}
	
	public void run() {
		try {
			this.serverSocket = new DatagramSocket(port);
		} catch (Exception e) {
			System.err.println("Exceptie la crearea DatagramSocket-ului: " + e);
			e.printStackTrace();
			return;
		}
		
		for (int i = 0; i < MaxRequests; i++) {
			System.out.println("[UDPServer] Waiting for a new request");
			
			try {
				byte[] buf = new byte[1024];
				DatagramPacket packet = new DatagramPacket(buf, buf.length);
				this.serverSocket.receive(packet);
				
				System.out.println("Am primit primul pachet " + new String (packet.getData()));
				
				
				String value = new String (packet.getData());
				
				String[] elemente = value.split(" ");
				
				System.out.println("Am tipul de comanda " + elemente[0]);
				if(elemente[0].trim().equalsIgnoreCase("put"))
				{
					int poziton =value.indexOf(":");
					String numberDim = value.substring(poziton+1);
					
					System.out.println("Am numarul " + numberDim);
					int dimFisier = Integer.parseInt("" + numberDim.trim() + "");
					byte[] bufFisier = new byte[dimFisier];
					DatagramPacket packetFisier = new DatagramPacket(bufFisier, bufFisier.length);
					this.serverSocket.receive(packetFisier);
					
					UDPServiceThread ust = new UDPServiceThread(this.serverSocket, packetFisier,dimFisier,vectorIdFisiere,"put",elemente[1].substring(elemente[1].indexOf('.')));
					ust.setStat(nPut, nGet, nFilter, nResize);
					ust.start();
					this.al.add(ust);
				}else
				{
					if(elemente[0].trim().equalsIgnoreCase("get"))
					{
						
						
						UDPServiceThread ust = new UDPServiceThread(this.serverSocket, packet,220,vectorIdFisiere,"get","test");
						ust.setStat(nPut, nGet, nFilter, nResize);
						ust.start();
						this.al.add(ust);
						
					}else{
						if(elemente[0].trim().equalsIgnoreCase("filter"))
						{
							UDPServiceThread ust = new UDPServiceThread(this.serverSocket, packet,220,vectorIdFisiere,"filter","test2");
							ust.setStat(nPut, nGet, nFilter, nResize);
							ust.start();
							this.al.add(ust);
						}else
						{
							if(elemente[0].trim().equalsIgnoreCase("resize"))
							{
								UDPServiceThread ust = new UDPServiceThread(this.serverSocket, packet,220,vectorIdFisiere,"resize","test3");
								ust.setStat(nPut, nGet, nFilter, nResize);
								ust.start();
								this.al.add(ust);
							}
						}
					}
				}
			} catch (Exception e) {
				System.err.println("Eroare la receive: " + e);
				e.printStackTrace();
			}
			
			try {
			//	apm.sendParameter("Servers", serverName, "nPut", nPut);
			//	apm.sendParameter("Servers", serverName, "nGet", nGet);
			//	apm.sendParameter("Servers", serverName, "nFilter", nFilter);
			//	apm.sendParameter("Servers", serverName, "nResize", nResize);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("Ajung aici dupa maxreguest");
		for (UDPServiceThread ust: this.al) {
			try {
				ust.join();
			} catch (Exception e) {}
		}
		
		System.out.println("Inchid socketul");
		try {
			this.serverSocket.close();
		} catch (Exception e) {
			System.err.println("DatagramSocket close exception: " + e);
			e.printStackTrace();
		}
		
		apm.stopIt();
	}
	
	
}
